package com.irdstudio.biz.rule.service.impl;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.biz.rule.service.facade.BizRuleAdapterService;
import com.irdstudio.biz.rule.dao.BizRuleAdapterDao;
import com.irdstudio.biz.rule.dao.domain.BizRuleAdapter;
import com.irdstudio.biz.rule.service.vo.BizRuleAdapterVO;
import com.irdstudio.sdk.beans.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 业务规则适配				<p>
 * @author AutoCoder
 * @date 2019-10-30
 */
@Service("bizRuleAdapterServiceImpl")
public class BizRuleAdapterServiceImpl implements BizRuleAdapterService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BizRuleAdapterServiceImpl.class);

	@Autowired
	private BizRuleAdapterDao bizRuleAdapterDao;
	
	/**
	 * 新增操作
	 */
	public int insertBizRuleAdapter(BizRuleAdapterVO inBizRuleAdapterVo) {
		logger.debug("当前新增数据为:"+ inBizRuleAdapterVo.toString());
		int num = 0;
		try {
			BizRuleAdapter bizRuleAdapter = new BizRuleAdapter();
			beanCopy(inBizRuleAdapterVo, bizRuleAdapter);
			num = bizRuleAdapterDao.insertBizRuleAdapter(bizRuleAdapter);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BizRuleAdapterVO inBizRuleAdapterVo) {
		logger.debug("当前删除数据条件为:"+ inBizRuleAdapterVo);
		int num = 0;
		try {
			BizRuleAdapter bizRuleAdapter = new BizRuleAdapter();
			beanCopy(inBizRuleAdapterVo, bizRuleAdapter);
			num = bizRuleAdapterDao.deleteByPk(bizRuleAdapter);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("根据条件:"+ inBizRuleAdapterVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BizRuleAdapterVO inBizRuleAdapterVo) {
		logger.debug("当前修改数据为:"+ inBizRuleAdapterVo.toString());
		int num = 0;
		try {
			BizRuleAdapter bizRuleAdapter = new BizRuleAdapter();
			beanCopy(inBizRuleAdapterVo, bizRuleAdapter);
			num = bizRuleAdapterDao.updateByPk(bizRuleAdapter);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("根据条件:"+ inBizRuleAdapterVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BizRuleAdapterVO queryByPk(BizRuleAdapterVO inBizRuleAdapterVo) {
		
		logger.debug("当前查询参数信息为:"+ inBizRuleAdapterVo);
		try {
			BizRuleAdapter queryBizRuleAdapter = new BizRuleAdapter();
			beanCopy(inBizRuleAdapterVo, queryBizRuleAdapter);
			BizRuleAdapter queryRslBizRuleAdapter = bizRuleAdapterDao.queryByPk(queryBizRuleAdapter);
			if (Objects.nonNull(queryRslBizRuleAdapter)) {
				BizRuleAdapterVO outBizRuleAdapterVo = beanCopy(queryRslBizRuleAdapter, new BizRuleAdapterVO());
				logger.debug("当前查询结果为:"+ outBizRuleAdapterVo.toString());
				return outBizRuleAdapterVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizRuleAdapterVO> queryAllOwner(BizRuleAdapterVO bizRuleAdapterVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BizRuleAdapterVO> list = null;
		try {
			List<BizRuleAdapter> bizRuleAdapters = bizRuleAdapterDao.queryAllOwnerByPage(bizRuleAdapterVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bizRuleAdapters.size());
			pageSet(bizRuleAdapters, bizRuleAdapterVo);
			list = (List<BizRuleAdapterVO>) beansCopy(bizRuleAdapters, BizRuleAdapterVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizRuleAdapterVO> queryAllCurrOrg(BizRuleAdapterVO bizRuleAdapterVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BizRuleAdapter> bizRuleAdapters = bizRuleAdapterDao.queryAllCurrOrgByPage(bizRuleAdapterVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bizRuleAdapters.size());
		List<BizRuleAdapterVO> list = null;
		try {
			pageSet(bizRuleAdapters, bizRuleAdapterVo);
			list = (List<BizRuleAdapterVO>) beansCopy(bizRuleAdapters, BizRuleAdapterVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizRuleAdapterVO> queryAllCurrDownOrg(BizRuleAdapterVO bizRuleAdapterVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BizRuleAdapter> bizRuleAdapters = bizRuleAdapterDao.queryAllCurrDownOrgByPage(bizRuleAdapterVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bizRuleAdapters.size());
		List<BizRuleAdapterVO> list = null;
		try {
			pageSet(bizRuleAdapters, bizRuleAdapterVo);
			list = (List<BizRuleAdapterVO>) beansCopy(bizRuleAdapters, BizRuleAdapterVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
}
